from another_version2 import *
import re


def copy_propagation(bloc):
    instr = bloc.instructions
    for index, elem in enumerate(instr):
        if index < len(instr)-1:
            el = instr[index+1]
            # examine sw followed by lw
            if "sw" in elem:
                if "lw" in el:
                    s3 = elem.split("\t")
                    l3 = el.split("\t")
                    if s3[-1] == l3[-1]:
                        # remove load word
                        print "=="
                        bloc.instructions.remove(el)

            # examine sb followed by lb
            elif "sb" in elem:
                if "lb" in el:
                    s3 = elem.split("\t")
                    l3 = el.split("\t")
                    if s3[-1] == l3[-1]:
                        # remove load word
                        print "=="
                        bloc.instructions.remove(el)

            # examine move followed by sw
            elif "move" in elem:   
                if ("sw" in el) or ("sb" in el):
                    m = re.split(r'[\t ,]', elem)
                    s = re.split(r'[\t ,]', el)
                    if m[2] == s[2]:
                        print "move"
                        instr[index+1] = el.replace(s[2], m[3])
                        bloc.instructions.remove(elem)
                
            # examine subu, addu, lw, lb followed by move
            elif (("subu" in elem) or ("addu" in elem) or ("lb" in elem)): # or ("lw" in elem)):
                if ("move" in el):
                    sal = re.split(r'[\t ,]', elem)
                    m = re.split(r'[\t ,]', el)
                    if m[3] == sal[2]:
                        print "sal", elem
                        instr[index] = elem.replace(sal[2], m[2])
                        instr.remove(el)

    return bloc

def constant_folding(bloc):
    instr = bloc.instructions
    for index, elem in enumerate(instr):
#		print "for",elem
        if "li" in elem:
            # get constant and register ,temp[1] is register
            temp = re.split(r'[\t ,]', elem)
            if index < len(instr)-1:
                e = instr[index+1]

                # check if register (temp[1]) is used in the next lines
                if ("bne" in e) or ("beq" in e):
                    t = re.split(r'[\t ,]', e)    
                    if temp[2] in t[2]:
                        print "if ##"
                        instr[index+1] = e.replace(t[2], temp[3])
                        bloc.instructions.remove(elem)
                    elif temp[2] in t[3]:
                        print "el ##"
                        instr[index+1] = e.replace(t[3], temp[3])
                        bloc.instructions.remove(elem)
                elif "addu" in e:
                    t = re.split(r'[\t ,]', e)
                    print "addu",t,"temp",temp

                    if temp[2] in t[3]:
                        print "if ##"
                        instr[index+1] = e.replace(t[3], temp[3])
                        bloc.instructions.remove(elem)
                    elif temp[2] in t[4]:
                        print "el ##"
                        instr[index+1] = e.replace(t[4], temp[3])
                        bloc.instructions.remove(elem)
    return bloc



if __name__ == "__main__":
    first_phase()

    global all_blocks
    f = open("test.s", "w")

    for index, block in enumerate(all_blocks):
        if index == 0:
            # write compiler directives to file
            for k in block.instructions:
                f.write(k+"\n")
        else:
            l_before = len(block.instructions)
            block = copy_propagation(block)
            block = constant_folding(block)

            for line in block.instructions:
                f.write(line+"\n")

            l_after = len(block.instructions)
    f.close()







